package br.ufmg.notservice.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Intent;
import br.ufmg.notservice.activity.NotificationReceivable;
import br.ufmg.ubicomp.event.EventObject;

public class NotificationEventServer {

	private static NotificationEventServer theServer;
	private List<EventObject> eventObjects;
	private List<NotificationReceivable> notificationListeners;

	private NotificationEventServer() {
		super();
		init();
	}

	private void init() {
		eventObjects = new ArrayList<EventObject>();
		notificationListeners = new ArrayList<NotificationReceivable>();
	}
	
	public static NotificationEventServer getInstance()
	{
		if (theServer == null)
		{
			theServer = new NotificationEventServer();
		}
		return theServer;
	}
	
	public void addNotificationListener(NotificationReceivable nr)
	{
		notificationListeners.add(nr);
	}
	
	public void notifyListeners(Intent intent)
	{
		for (NotificationReceivable nr: notificationListeners)
		{
			nr.showNotification(intent);
		}
	}
	
	public void addEvent(EventObject eo)
	{
		eventObjects.add(eo);
	}
	
	public EventObject getEvent(long id)
	{
		for (EventObject eo: eventObjects)
		{
			if (eo.getId() == id)
			{
				return eo;
			}
		}
		return null;
	}
	
	public List<EventObject> getEvents()
	{
		return Collections.unmodifiableList(eventObjects);
	}
	
	public void emptyListOfEvents()
	{
		eventObjects.clear();
	}
}
